@extends('layout.layoutv5')
@section('content')
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-title">
                    <h5>货道列表</h5>
                </div>
                <div class="ibox-content">

                    <table class="table table-striped">
                        <thead>
                        <tr>
                            <th>#</th>
                            <th>商品</th>
                            <th>进价</th>
                            <th>售价</th>
                            <th>操作</th>
                        </tr>
                        </thead>
                        <tbody>
                        <input id="mc_id" type="text" hidden value="{{$machineId}}">
                        @foreach ($slotList as $slot)
                        <tr>

                            <td class="_huodao">货道<small class="_td_slot_id">{{ $slot['slot_id'] }}</small></td>
                            <td>{{ $slot->goods->goods_name }}</td>
                            <td>{{ $slot['good_cost'] }}</td>
                            <td>{{ $slot['sale_price'] }}</td>
                            <td>
                                <a href="javascript:void(0);" class="_motal_target" {{--data-toggle="modal" data-target="#myModal"--}}>编辑货道
                                </a>
                            </td>
                        </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <div>
        <div class="modal inmodal" id="myModal" tabindex="-1" role="dialog" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content animated bounceInRight">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">关闭</span>
                        </button>
                        <h6 class="modal-title" id="motal_title">货道</h6>
                    </div>
                    <input type="text" hidden id="_slot_id">
                    <div class="modal-body">
                        <div class="form-group">
                            <label>商品id</label>
                            <div class="row">
                                <div class="col-sm-12">
                                    <div class="input-group">
                                        <input type="text" class="form-control" id="_good_id">
                                        <div class="input-group-btn">
                                            <button type="button" class="btn btn-white dropdown-toggle" data-toggle="dropdown">
                                                <span class="caret text-warning" id="error_msg"></span>
                                            </button>
                                            <ul class="dropdown-menu dropdown-menu-right" role="menu">
                                            </ul>
                                        </div>
                                        <!-- /btn-group -->
                                    </div>
                                </div>
                            </div>
                        </div>



                        <div class="form-group">
                            <label>商品单价</label>
                            <input id="_price" placeholder="输入商品单价"
                                   class="form-control">
                        </div>
                        <div class="form-group">
                            <label>商品售价</label>
                            <input id="_sale_price"
                                   placeholder="输入商品售价"
                                   class="form-control">
                        </div>



                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-white" data-dismiss="modal">关闭</button>
                        <button type="button" class="btn btn-primary" id="save_solt">保存</button>
                    </div>
                </div>
            </div>
        </div>

    </div>

@endsection
@section('js_block')
    <script src="/js/plugins/echarts/echarts-all.js"></script>
    <script src="/js/plugins/suggest/bootstrap-suggest.min.js"></script>

    <script>
        $(document).ready(function () {
            $("._motal_target").click(function () {
                var huodao = $(this).parents('tr').find('._huodao').html();
                var slot_id = $(this).parents('tr').find('._td_slot_id').html()
                $("#_slot_id").val(slot_id);
                $("#motal_title").html(huodao);
                $('#myModal').modal('show');
            });

            //淘宝搜索建议测试
            var taobaoBsSuggest = $("#_good_id").bsSuggest({
              //  indexId: 2, //data.value 的第几个数据，作为input输入框的内容
              //  indexKey: 1, //data.value 的第几个数据，作为input输入框的内容
                allowNoKeyword: false, //是否允许无关键字时请求数据
                multiWord: false, //以分隔符号分割的多关键字支持
                separator: ",", //多关键字支持时的分隔符，默认为空格
                keyField:'id',
                getDataMethod: "url", //获取数据的方式，总是从 URL 获取
                effectiveFieldsAlias: {
                    num: "序号",
                    id: "id",
                    goods_name: "名称",
                    price: "价格"
                },
                showHeader: true,
                url: '/goods/selectGoodsByName?good_name=',
                /*优先从url ajax 请求 json 帮助数据，注意最后一个参数为关键字请求参数*/
               // jsonp: 'callback',
                /*如果从 url 获取数据，并且需要跨域，则该参数必须设置*/
                processData: function (json) { // url 获取数据时，对数据的处理，作为 getData 的回调函数
                    var i, len, data = {
                        value: []
                    };

                    if (!json || !json.result || json.result.length == 0) {
                        return false;
                    }

                    console.log(json);
                    len = json.result.length;

                    for (i = 0; i < len; i++) {
                        data.value.push({
                            "num": i+1,
                            "id": json.result[i]['id'],
                            "goods_name": json.result[i]['goods_name'],
                            "price": json.result[i]['price']
                        });
                    }
                    console.log(data);
                    return data;
                }
            }).on('onSetSelectValue', function (e, keyword) {
                console.log('onSetSelectValue: ', keyword);
            });
            
            $("#save_solt").click(function () {
                var _good_id = $("#_good_id").val();
                var price = $("#_price").val();
                var sale_price = $("#_sale_price").val();
                var mc_id = $("#mc_id").val();
                var _slot_id = $("#_slot_id").val();
                if(!isNumber(_good_id)){
                    alert('整数商品id');
                    return false;
                }
                if(!isNumber(price)){
                    alert('进价为数字');
                    return false;
                }
                if(!isNumber(sale_price)){
                    alert('销售价格为数字');
                    return false;
                }

                $.ajax({
                    'type':'POST',
                    'data':{slot_id:_slot_id, mc_id:mc_id,goods_id:_good_id,good_cost:price,sale_price:sale_price },
                    'url':'/machine/doEditMachineSlots',
                    'success':function (data) {
                        console.log(data);
                        if(data.code ==0){
                            $("#myModal input").val("");
                            $('#myModal').modal('hide');
                        }else{
                            $('#error_msg').html(data.code);

                        }

                    }
                });


            });


            function isNumber(val){
                var regPos = /^\d+(\.\d+)?$/; //非负浮点数
                var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
                if(regPos.test(val) || regNeg.test(val)){
                    return true;
                }else{
                    return false;
                }

            }
        });
    </script>
@endsection
